<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
    <style>
        .box {
            width: 80%;
            margin: 10px auto;
            min-height: 320px;
            border: 1px darkgoldenrod solid;
            border-radius: 10px;
        }

        .router-link-exact-active,
        .router-link-active {
            background-color: darkgoldenrod;
            color: black;
        }
    </style>
</head>

<body>
    <div id="app">
        <h1>{{title}}</h1>
        <hr>
        <!-- 5.路由出口 在哪里显示 路由组件 -->
        <div class="box">
            <router-view></router-view>
        </div>

        <!-- 6.路由导航 声明式导航 -->
        <div>
            <!-- hash值#XXX -->
            <!-- <router-link to="/wechat#happy">首页</router-link>
            <router-link to="/contact/玛卡巴卡">通讯录</router-link>
            <router-link to="/find/擦/info/学不会学不会?address=武汉">发现</router-link>
            <router-link to="/find/擦/info/学会了学会了?address=非洲">发现2</router-link>
            <router-link to="/mine?phone=13235566547">个人中心</router-link>
            <router-link to="/not-found">未定义</router-link> -->

            <router-link :to="{name:'wechat',hash:'#happy'}">首页</router-link>
            <router-link to="/wechat/admin">超管</router-link>
            <router-link :to="{name:'contact',params:{who:'玛卡巴卡'}}">通讯录</router-link>
            <router-link :to="{name:'find',query:{address:'武汉'},params:{uid:'擦',time:'学不会学不会'}}">发现</router-link>
            <router-link :to="{name:'find',query:{address:'非洲'},params:{uid:'擦',time:'学会了学会了'}}">发现2</router-link>
            <router-link :to="{name:'mine',query:{phone:110}}">个人中心</router-link>
            <router-link to="/not-found">未定义</router-link>
        </div>
    </div>


    <template id="wechat">
        <div>
            <h2>微信主页</h2>
            <h2>$route.query.XXX 获取?拼接的参数 (获取查询参数)</h2>
            <h2>$route.params.XXX 获取 : 声明的参数 (路由参数 动态路由)</h2>
            <h2>$route.name 路由名称 {{$route.name}}</h2>
            <h2>$route.path 路由路径(不包含search) {{$route.path}}</h2>
            <h2>$route.fullPath 路由全部地址 {{$route.fullPath}}</h2>
            <h2>$route.hash hash值 # {{$route.hash}}</h2>
            <h2>$route.meta 路由标记信息 {{$route.meta}}</h2>
            <h2></h2>
        </div>
    </template>

    <template id="contact">
        <div>
            <h2>通讯录</h2>
            <h2>who:{{$route.params.who}}</h2>
        </div>
    </template>

    <template id="find">
        <div>
            <h2>发现</h2>
            <h2>地址:{{$route.query.address}}</h2>
            <h2>uid:{{$route.params.uid}}</h2>
            <h2>time:{{$route.params.time}}</h2>
        </div>
    </template>

    <template id="mine">
        <div>
            <h2>个人中心</h2>
            <h2>phone: {{$route.query.phone}}</h2>
        </div>
    </template>

    <template id="notFound">
        <div>
            <h2>没有找到页面嘞</h2>
        </div>
    </template>

    <script>
        // 0.如何使用模块化机制编程 ,导入Vue和VueRouter,要调用Vue.use(VueRouter)

        // 1.定义(路由)组件
        const Wechat = {
            template: "#wechat"
        }
        const Contact = {
            template: "#contact"
        }
        const Find = {
            template: "#find"
        }
        const Mine = {
            template: "#mine"
        }

        const NotFound = {
            template: "#notFound"
        }

        // 2.定义路由 定义好路由的匹配规则
        // 每个路由都应该隐射一个组件
        const routes = [
            { path: '/', component: NotFound },/* 如果是不存在的组件 默认返回404 */
            { path: '/wechat', component: Wechat, name: 'wechat', meta: { like: '666' } },
            {
                path: '/wechat/admin', component: {
                    template: "<h1>admin -- admin -- admin 我是超管</h1>"
                }, name: 'admin'
            },
            { path: '/contact/:who', component: Contact, name: 'contact' },   /* name:XXX  别名*/
            { path: '/find/:uid/info/:time?', component: Find, name: 'find', alias: "/myfriends" },
            { path: '/mine', component: Mine, name: 'mine', },
            { path: '/404', component: NotFound, alias: "/notfound" },/* alias 设置别名 */
            // { path: '*', component: NotFound },
            { path: "*", redirect: "/404" }

        ]

        // 3.创建路由实例对象
        const router = new VueRouter({
            routes,
        })

        // 4.挂载到根实例
        const vm = new Vue({
            router,
            watch: {
                '$route': function (to, from) {
                    console.log(to);
                    console.log(from);
                }
            },
            data: {
                title: "Vue Router 入门学习",
            },
            mounted() {
                console.log(this.$router);
                console.log(this.$route);

            }
        }).$mount("#app")
    </script>
</body>

</html>